<script setup name="index">
import { getStatistics1, getStatistics2 } from '@/api/index.js'
import { ref } from 'vue'
import CountTo from '@/components/CountTo.vue'
import IndexNavs from '@/components/IndexNavs.vue'
import IndexChart from '@/components/IndexChart.vue'
import IndexCard from '@/components/IndexCard.vue'

const panels = ref([])
const goods = ref([])
const order = ref([])

const loadData = async () => {
  const res = await Promise.all([getStatistics1(), getStatistics2()])
  panels.value = res[0].panels
  goods.value = res[1].goods
  order.value = res[1].order
}
loadData()
</script>

<template>
  <div>
    <el-row :gutter="10">
      <!-- 骨架屏 -->
      <template v-if="panels.length == 0">
        <el-col :sm="6" v-for="i in 4" :key="i">
          <el-skeleton style="width: 100%" animated loading>
            <template #template>
              <el-card shadow="hover" style="height: 213px">
                <template #header>
                  <div>
                    <el-skeleton-item variant="text" style="width: 50%" />
                    <el-skeleton-item variant="text" style="width: 10%" />
                  </div>
                </template>
                <el-skeleton-item variant="h3" style="width: 80%" />
                <el-divider />
                <div>
                  <el-skeleton-item variant="text" style="width: 50%" />
                  <el-skeleton-item variant="text" style="width: 10%" />
                </div>
              </el-card>
            </template>
          </el-skeleton>
        </el-col>
      </template>
      <!-- 统计数据 -->
      <el-col v-for="(item, index) in panels" :key="index" :sm="6" :offset="0">
        <el-card :body-style="{ 'text-align': 'center' }" v-rotateCard>
          <template #header>
            <div class="card-header">
              <span class="card-title">{{ item.title }}</span>
              <el-tag :type="item.unitColor" effect="plain">{{ item.unit }}</el-tag>
            </div>
          </template>
          <span class="card-value">
            <CountTo :value="item.value" />
          </span>
          <el-divider />
          <div class="card-body">
            <span>{{ item.subTitle }}</span>
            <span>{{ item.subValue }}</span>
          </div>
        </el-card>
      </el-col>
    </el-row>
    <!-- 快捷导航栏 -->
    <IndexNavs />
    <!-- 图表 -->
    <el-row :gutter="10" class="chart">
      <el-col :sm="12">
        <IndexChart />
      </el-col>
      <!-- 骨架屏 -->
      <template v-if="goods.length === 0">
        <el-col :sm="12">
          <el-skeleton v-for="i in 2" :key="i" style="width: 100%; margin-bottom: 10px;" animated loading>
            <template #template>
              <el-card shadow="hover" style="height: 193px;">
                <template #header>
                  <div class="skeleton-header">
                    <el-skeleton-item variant="text" style="width: 36%;" />
                    <el-skeleton-item variant="text" style="width: 28%" />
                  </div>
                </template>
                <div class="skeleton-body">
                  <el-skeleton-item class="skeleton-body-item" variant="image" style="width: 100%; height: 90px;" />
                  <el-skeleton-item class="skeleton-body-item" variant="image" style="width: 100%; height: 90px;" />
                  <el-skeleton-item class="skeleton-body-item" variant="image" style="width: 100%; height: 90px;" />
                  <el-skeleton-item class="skeleton-body-item" variant="image" style="width: 100%; height: 90px;" />
                </div>
              </el-card>
            </template>
          </el-skeleton>
        </el-col>
      </template>
      <el-col v-else :sm="12" :offset="0">
        <IndexCard class="goods" title="店铺及商品提示" tip="店铺及商品提示" :btns="goods" />
        <IndexCard class="order" title="交易提示" tip="需要立即处理的交易订单" :btns="order" />
      </el-col>
    </el-row>
  </div>
</template>

<style lang="scss" scoped>
.el-col {
  margin-bottom: 10px;
  font-size: var(--text-sm);

  .el-card {
    border: 0;

    .card-header,
    .card-body {
      white-space: nowrap;
      display: flex;
      justify-content: space-between;
    }

    .card-value {
      color: var(--gray500);
      font-size: var(--text-3xl);
      font-weight: bold;
    }

    .card-body {
      color: var(--gray500);
    }
  }
}

.chart {
  margin-top: 10px;

  .skeleton-header {
    display: flex;
    justify-content: space-between;
  }

  .skeleton-body {
    display: flex;

    &-item {
      margin: 0 6px;
      border-radius: 4px;
    }
  }

  .goods {
    margin-bottom: 10px;
  }
}
</style>
